﻿/*
Commerce by Onex – eCommerce software platform (www.ecommerce.byonex.com)
Copyright (C) 2008-2010 by Onex Group

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 */
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using ShopBLL;
using ShopModel;

public partial class ProductsComparer : BasePage
{
    #region VARIABLES

    List<ProductProperty> CompareProperties = new List<ProductProperty>();
    List<Products> ProductsToCompare = new List<Products>();
    List<int> IDs;

    #endregion VARIABLES

    #region METHODS

    protected void GetProductsToCompare()
    {
        foreach (int id in IDs)
        {
            Products p = ProductsManager.Instance.GetProduct(id, ShopBLL.CommonTools.CurrentLanguageID);

            if (p != null)
            {
                foreach (ProductProperty pp in p.PropertyList)
                {
                    if (!CompareProperties.Exists(delegate(ProductProperty prop) { return prop.Id == pp.Id; }))
                        CompareProperties.Add(pp);
                }

                ProductsToCompare.Add(p);
            }
        }
    }

    protected void BindProperties()
    {
        rptProductsCompare.DataSource = CompareProperties;
        rptProductsCompare.DataBind();
    }

    protected void BindPropertyValues()
    {
        HtmlTableRow tMiniature = (HtmlTableRow)rptProductsCompare.Controls[0].FindControl("tMiniature");
        HtmlTableRow tName = (HtmlTableRow)rptProductsCompare.Controls[0].FindControl("tName");
        HtmlTableRow tProducer = (HtmlTableRow)rptProductsCompare.Controls[0].FindControl("tProducer");
        HtmlTableRow tPrice = (HtmlTableRow)rptProductsCompare.Controls[0].FindControl("tPrice");

        foreach (Products p in ProductsToCompare)
        {
            if (tMiniature != null)
            {
                HtmlTableCell tc = new HtmlTableCell();
                if (p.Pictures.Count > 0)
                {
                    HtmlImage image = new HtmlImage();
                    image.Src = p.Pictures[0].Miniature.VirtualPath;
                    tc.Controls.Add(image);
                }
                tMiniature.Cells.Add(tc);
            }

            if (tName != null)
            {
                HtmlTableCell tc = new HtmlTableCell();
                HtmlAnchor a = new HtmlAnchor();

                ImageButton ibRemoveProduct = new ImageButton();

                a.InnerText = p.Name;
                a.HRef = p.IDproduct.ToString() + "," + Resources.Language.Product.ToLower() + ".aspx";

                ibRemoveProduct.ImageUrl = "~/gfx/remove.png";
                ibRemoveProduct.CommandArgument = p.IDproduct.ToString();
                ibRemoveProduct.CommandName = "Remove";
                ibRemoveProduct.OnClientClick = "var shidden = document.getElementById('" + hfToDelete.ClientID + "'); shidden.value='" + p.IDproduct.ToString() + "';";
                ibRemoveProduct.Width = new Unit(15, UnitType.Pixel);
                ibRemoveProduct.Height = new Unit(15, UnitType.Pixel);


                tc.Controls.Add(a);
                tc.Controls.Add(ibRemoveProduct);
                tName.Cells.Add(tc);
            }

            if (tProducer != null)
            {
                HtmlTableCell tc = new HtmlTableCell();

                tc.InnerText = p.Producer.Name;

                tProducer.Cells.Add(tc);
            }

            if (tPrice != null)
            {
                HtmlTableCell tc = new HtmlTableCell();

                if (p.SpecialOfferPriceBrutto != null)
                    tc.InnerHtml = p.SpecialOfferPriceBrutto.ToString() + "&nbsp;<font style=\"text-decoration: line-through; color: Red;\">" + p.PriceBrutto.ToString() + "</font>";
                else
                    tc.InnerText = p.PriceBrutto.ToString();

                tPrice.Cells.Add(tc);
            }
        }

        foreach (RepeaterItem rItem in rptProductsCompare.Items)
        {
            HiddenField hfPropertyID = (HiddenField)rItem.FindControl("hfPropertyID");
            HtmlTableRow tItemRow = (HtmlTableRow)rItem.FindControl("tItemRow");

            if (hfPropertyID != null)
            {
                ProductProperty currentProperty = CompareProperties.Find(delegate(ProductProperty pro) { return pro.Id == int.Parse(hfPropertyID.Value); });

                foreach (Products p in ProductsToCompare)
                {
                    HtmlTableCell tc = new HtmlTableCell();

                    if (currentProperty.TypeOfProperty == PropertyType.MultiDictionary)
                    {
                        List<ProductProperty> pp = p.PropertyList.FindAll(delegate(ProductProperty pro) { return pro.Id == currentProperty.Id; });

                        if (pp.Count > 0)
                        {
                            foreach (ProductProperty prop in pp)
                            {
                                tc.InnerHtml += prop.Value + "<br />";
                            }

                            tc.InnerHtml.Remove(tc.InnerHtml.Length - 6);
                        }
                    }
                    else
                    {
                        ProductProperty pp = p.PropertyList.Find(delegate(ProductProperty pro) { return pro.Id == currentProperty.Id; });

                        if (pp != null)
                        {
                            if (pp.TypeOfProperty == PropertyType.Numeric)
                                tc.InnerText = pp.NumericValue > 0 ? pp.NumericValue.ToString() : "";
                            else
                                tc.InnerText = pp.Value;
                        }
                    }

                    tItemRow.Cells.Add(tc);
                }
            }
        }
    }

    #endregion METHODS

    #region EVENTS

    protected void Sort_Command(object sender, CommandEventArgs e)
    {
        string PreviousSortCriteria = Session["PreviousSortCriteria"] != null ? (string)Session["PreviousSortCriteria"] : "";
        int sDirection = int.Parse(hfSortDirection.Value);
        Session["PreviousSortCriteria"] = e.CommandName + (string)e.CommandArgument;
        bool PropertySort = false;

        switch (e.CommandName)
        {
            case "SortName":
                if (PreviousSortCriteria.Contains("SortName"))
                    sDirection = 1;
                else
                    sDirection *= -1;

                ProductsToCompare.Sort(delegate(Products p1, Products p2) { return String.Compare(p1.Name, p2.Name) * sDirection; });
                break;
            case "SortNameAscending":
                sDirection = 1;

                ProductsToCompare.Sort(delegate(Products p1, Products p2) { return String.Compare(p1.Name, p2.Name) * sDirection; });
                break;
            case "SortNameDescending":
                sDirection = -1;

                ProductsToCompare.Sort(delegate(Products p1, Products p2) { return String.Compare(p1.Name, p2.Name) * sDirection; });
                break;
            case "SortProducer":
                if (PreviousSortCriteria != "SortProducer")
                    sDirection = 1;
                else
                    sDirection *= -1;

                ProductsToCompare.Sort(delegate(Products p1, Products p2) { return String.Compare(p1.Producer.Name, p2.Producer.Name) * sDirection; });
                break;
            case "SortProducerAscending":
                sDirection = 1;

                ProductsToCompare.Sort(delegate(Products p1, Products p2) { return String.Compare(p1.Producer.Name, p2.Producer.Name) * sDirection; });
                break;
            case "SortProducerDescending":
                sDirection = -1;

                ProductsToCompare.Sort(delegate(Products p1, Products p2) { return String.Compare(p1.Producer.Name, p2.Producer.Name) * sDirection; });
                break;
            case "SortPrice":
                if (PreviousSortCriteria != "SortPrice")
                    sDirection = 1;
                else
                    sDirection *= -1;

                ProductsToCompare.Sort(delegate(Products p1, Products p2) { return decimal.Compare(p1.PriceBrutto, p2.PriceBrutto) * sDirection; });
                break;
            case "SortPriceAscending":
                sDirection = 1;

                ProductsToCompare.Sort(delegate(Products p1, Products p2) { return decimal.Compare(p1.PriceBrutto, p2.PriceBrutto) * sDirection; });
                break;
            case "SortPriceDescending":
                sDirection = -1;

                ProductsToCompare.Sort(delegate(Products p1, Products p2) { return decimal.Compare(p1.PriceBrutto, p2.PriceBrutto) * sDirection; });
                break;
            case "SortProperty":
                if (PreviousSortCriteria.Contains("SortProperty") && PreviousSortCriteria.EndsWith((string)e.CommandArgument))
                    sDirection = 1;
                else
                    sDirection *= -1;
                PropertySort = true;
                break;

            case "SortPropertyAscending":
                sDirection = 1;
                PropertySort = true;
                break;

            case "SortPropertyDescending":
                sDirection = -1;
                PropertySort = true;
                break;

        }


        if (PropertySort)
        {
            ProductProperty sortProperty = CompareProperties.Find(delegate(ProductProperty pro) { return pro.Id == int.Parse((string)e.CommandArgument); });

            if (sortProperty.TypeOfProperty == PropertyType.Numeric)
            {
                ProductsToCompare.Sort(delegate(Products p1, Products p2)
                {
                    ProductProperty p1pro = p1.PropertyList.Find(delegate(ProductProperty pro) { return pro.Id == sortProperty.Id; });
                    ProductProperty p2pro = p2.PropertyList.Find(delegate(ProductProperty pro) { return pro.Id == sortProperty.Id; });

                    if (p1pro != null && p1pro.NumericValue != null && p2pro != null && p2pro.NumericValue != null)
                        return decimal.Compare((decimal)p1pro.NumericValue, (decimal)p2pro.NumericValue) * sDirection;
                    else
                        if ((p1pro == null || p1pro.NumericValue == null) && (p2pro == null || p2pro.NumericValue == null))
                            return 0;
                        else
                            if ((p1pro == null || p1pro.NumericValue == null) && (p2pro != null && p2pro.NumericValue != null))
                                return 1;
                            else if ((p1pro != null && p1pro.NumericValue != null) && (p2pro == null || p2pro.NumericValue == null))
                                return -1;

                    return 0;
                });
            }
            else if (sortProperty.TypeOfProperty == PropertyType.MultiDictionary)
            {
                ProductsToCompare.Sort(delegate(Products p1, Products p2)
                {
                    List<ProductProperty> p1pro = p1.PropertyList.FindAll(delegate(ProductProperty pro) { return pro.Id == sortProperty.Id; });
                    List<ProductProperty> p2pro = p2.PropertyList.FindAll(delegate(ProductProperty pro) { return pro.Id == sortProperty.Id; });

                    if (p1pro != null && p1pro.Count > 0 && p2pro != null && p2pro.Count > 0)
                        return (p1pro.Count - p2pro.Count) * sDirection;
                    else
                        if ((p1pro == null || p1pro.Count == 0) && (p2pro == null || p2pro.Count == 0))
                            return 0;
                        else
                            if ((p1pro == null || p1pro.Count == 0) && (p2pro != null && p2pro.Count > 0))
                                return 1;
                            else if ((p1pro != null && p1pro.Count > 0) && (p2pro == null || p2pro.Count == 0))
                                return -1;

                    return 0;
                });
            }
            else
            {
                ProductsToCompare.Sort(delegate(Products p1, Products p2)
                {
                    ProductProperty p1pro = p1.PropertyList.Find(delegate(ProductProperty pro) { return pro.Id == sortProperty.Id; });
                    ProductProperty p2pro = p2.PropertyList.Find(delegate(ProductProperty pro) { return pro.Id == sortProperty.Id; });

                    if (p1pro != null && p1pro.Value != string.Empty && p2pro != null && p2pro.Value != string.Empty)
                        return string.Compare(p1pro.Value, p2pro.Value) * sDirection;
                    else
                        if ((p1pro == null || p1pro.Value == string.Empty) && (p2pro == null || p2pro.Value == string.Empty))
                            return 0;
                        else
                            if ((p1pro == null || p1pro.Value == string.Empty) && (p2pro != null && p2pro.Value != string.Empty))
                                return 1;
                            else if ((p1pro != null && p1pro.Value != string.Empty) && (p2pro == null || p2pro.Value == string.Empty))
                                return -1;

                    return 0;
                });
            }
        }


        hfSortDirection.Value = sDirection.ToString();
        //BindPropertyValues();

        IDs.Clear();

        foreach (Products p in ProductsToCompare)
            IDs.Add(p.IDproduct);

        Session["ProductsToCompare"] = IDs;

    }

    #endregion EVENTS

    #region PAGE EVENTS

    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["ProductsToCompare"] != null)
        {
            IDs = (List<int>)Session["ProductsToCompare"];

            if (hfToDelete.Value != string.Empty)
            {
                IDs.Remove(int.Parse(hfToDelete.Value));
                Session["ProductsToCompare"] = IDs;
            }

            if (IDs != null && IDs.Count > 0)
            {
                GetProductsToCompare();
                BindProperties();
            }
            else
            {
                Response.Redirect(Resources.Language.UrlDefault.ToLower() + ".aspx");
            }
        }
        else
        {
            Response.Redirect(Resources.Language.UrlDefault.ToLower() + ".aspx");
        }
    }

    protected void Page_LoadComplete(object sender, EventArgs e)
    {
        if (IDs != null && IDs.Count > 0)
        {
            BindPropertyValues();
        }
        else
        {
            rptProductsCompare.Visible = false;
        }
    }
    #endregion PAGE EVENTS
}
